<?php


namespace App\Admin\Forms\Settings;


use App\Setting;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

abstract class BaseForm extends Form
{
    public $title = '';
    protected $type = '';
    public $settings = [];

    public function handle(Request $request)
    {
        $data = collect($request->only(array_keys($this->settings)))->map(function ($value, $slug) {
            if ($slug == 'sync_patent_type') {
                $value = collect($value)->filter()->implode(',');
            }
            return [
                'name' => data_get($this->settings, $slug),
                'slug' => $slug,
                'value' => $value,
            ];
        })->values();
        Setting::createOrUpdate($data, $this->type);
        admin_toastr('操作完成');
        return back();
    }

    abstract public function form();

    public function data()
    {
        return Setting::getSettings($this->type)->pluck('value', 'slug')->map(function ($value, $slug) {
            if ($slug == 'sync_patent_type') {
                $value = explode(',', $value);
            }
            return $value;
        });
    }

}
